﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using Hidistro.Entities.Commodities;
    using Hidistro.SaleSystem.Catalog;
    using JSShop.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class YB_Brand_CategoryList: WebControl
    {
        private int brandId;
        private int cid;
        private string valueStr = string.Empty;

        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pn="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }

        protected override void OnInit(EventArgs e)
        {
            int.TryParse(this.Context.Request.QueryString["brandId"], out this.brandId);
            int.TryParse(this.Context.Request.QueryString["cid"], out this.cid);
            base.OnInit(e);
        }

        private string html()
        {
            StringBuilder sb = new StringBuilder();
            List<JSShop.Model.Hishop_Categories> brandCategories = new Juse.Interface.CategoryBrowser().GetBrandCategoryList(this.brandId, 16);
            if ((brandCategories != null) && (brandCategories.Count > 0))
            {
                if(string.IsNullOrEmpty(this.Context.Request.QueryString["Staticize"]))
                { 
                    sb.AppendFormat("<a href=\"{0}\"{1}>全部</a>", this.CreateUrl("cid", ""), this.cid == 0 ? " class=\"kkokk\"" : "");
                    foreach (Hishop_Categories row in brandCategories)
                    {
                        sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", this.CreateUrl("cid", row.CategoryId.ToString()), row.Name, this.cid == ((int)row.CategoryId) ? " class=\"kkokk\"" : "");
                    }
                }
                else
                {
                    int cid;
                    int brand;
                    int filter;
                    int rank;

                    //string model = "Brand_detail-{0}_cid-{1}_filter-0_rank-0.html";
                    string model = "B{0}C{1}F0R0.html";

                    string url;
                    int.TryParse(this.Page.Request.QueryString["cid"], out cid);
                    int.TryParse(this.Page.Request.QueryString["brandId"], out brand);
                    int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                    int.TryParse(this.Page.Request.QueryString["rank"], out rank);

                    url = string.Format(model, brand, 0);

                   // string url = string.Format("Brand_detail-{0}_cid-0_filter-0_rank-{1}.html", brandId,rank);
                    sb.AppendFormat("<a href=\"{0}\"{1}>全部</a>", url, this.cid == 0 ? " class=\"kkokk\"" : "");
                    foreach (Hishop_Categories row in brandCategories)
                    {
                        url = string.Format(model, brandId, row.CategoryId);
                        sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", url, row.Name, this.cid == ((int)row.CategoryId) ? " class=\"kkokk\"" : "");
                    }
                }
            }

            return sb.ToString();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.html());
        }


    }
}

